package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.entity.SpuInfoEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.SkuInfoDao;
import com.guli.gulimall.product.entity.SkuInfoEntity;
import com.guli.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> w = new QueryWrapper<>();
        String key = (String) params.get("key");
        String catalogId = (String) params.get("catalogId");
        String brandId = (String) params.get("brandId");
        String max = (String) params.get("max");
        String min = (String) params.get("min");
        if (StringUtils.isNotEmpty(key)) {
            w.and(qw->
                    qw.like("sku_name",key).or().eq("sku_id",key)
            );
        }
        if (StringUtils.isNotEmpty(catalogId)&&!"0".equals(catalogId)){
            w.eq("catalog_id",catalogId);
        }
        if (StringUtils.isNotEmpty(brandId)&&!"0".equals(brandId)){
            w.eq("brand_id",brandId);
        }
        if (StringUtils.isNotEmpty(min)){
            w.ge("price",min);
        }
        if (StringUtils.isNotEmpty(max)&&!"0".equals(max)){
            w.le("price",max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                w
        );

        return new PageUtils(page);
    }

}
